package cn.aposoft.mq.kafka.admin.cluster;

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.Config;
import org.apache.kafka.clients.admin.DescribeConfigsResult;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.config.ConfigResource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class DescribeConfigs {
    public static void main(String[] args) {
        Properties p = new Properties();
        p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "aposoft.cn:10091,aposoft.cn:10092,aposoft.cn:10093");// kafka地址，多个地址用逗号分割
        try (Admin admin = Admin.create(p);) {
            Collection<ConfigResource> resources = new ArrayList<>();
            ConfigResource resource1 = new ConfigResource(ConfigResource.Type.TOPIC, "test-new");
            // BROKER 的name是 broker.id
            ConfigResource resource2 = new ConfigResource(ConfigResource.Type.BROKER, "91");
            // BROKER_LOGGER 的name是 broker.id
            ConfigResource resource3 = new ConfigResource(ConfigResource.Type.BROKER_LOGGER, "91");
            resources.add(resource1);
            resources.add(resource2);
            resources.add(resource3);

            DescribeConfigsResult result = admin.describeConfigs(resources);
            KafkaFuture<Map<ConfigResource, Config>> future = result.all();
            Map<ConfigResource, Config> entries = future.get();
            System.out.println(entries);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
